package com.rimi.service.impl;

import com.rimi.bean.Video;
import com.rimi.cache.VideoCache;
import com.rimi.cache.AdminCache;
import com.rimi.dao.VideoDao;
import com.rimi.service.VideoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * InnoDB free: 11264 kB; (`user_id`) REFER `owl_video/user`(`user_id`); (`menu_id` 服务实现类
 * </p>
 *
 * @author sky
 * @since 2018-06-06
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoDao, Video> implements VideoService {
    @Autowired
    private VideoDao videoDao;
    @Autowired
    private AdminCache adminCache;
    @Autowired
    private VideoService videoService;
    @Autowired
    private VideoCache videoCache;

    @Override
    public Video getVideoById(Integer id) {
        Video v = videoCache.getVideoByIdFromCache(id);
        if(v!=null){
            return v;
        }else {
            v=videoDao.getVideoById(id);
            videoCache.addVideo(v);
        }
        return v;
    }




    //查询视频菜单获取图片列表
    @Override
    public List<Video> getVideoList() {
        List<Video> videoList = adminCache.getCacheList("Video",Video.class);
        //缓存中是否有数据
        if(videoList!=null&&!videoList.isEmpty()){
            return videoList;
        }
        //如果没有数据
        List<Video> videoList1 = videoService.selectList(null);
        //将查询结果缓存到redis中
        adminCache.addCacheList("Video", videoList1);
        return videoList1;

    }

    @Override
    public void collectVideo(Integer userId, Integer videoId) {
        videoDao.collectVideo(userId,videoId);
    }

    @Override
    public void hotUp(Video v) {
        v.setVideoHot(v.getVideoHot()+1);
        videoCache.addVideo(v);
    }
}
